package com.bangcommunity.bbframe.common.base.attribute;

import java.util.Date;

/**
 * @author tanghc
 */
public abstract class AttributeHelper {
    public static String convertToJsonString(IAttributeAssemble attributeAssemble) {
        StringBuilder stringBuilder = new StringBuilder(512);
        stringBuilder.append("{");
        if (null != attributeAssemble && null != attributeAssemble.attibutes()) {
            int i = 0;
            for (IAttributeDesc attributeDesc : attributeAssemble.attibutes()) {
                if (i++ != 0) {
                    stringBuilder.append(",");
                }
                stringBuilder.append("\"");
                stringBuilder.append(attributeDesc.getName());
                stringBuilder.append("\":");
                if (attributeDesc.getCanConvertToString()) {
                    stringBuilder.append("\"");
                }
                stringBuilder.append(attributeDesc.getValue());
                if (attributeDesc.getCanConvertToString()) {
                    stringBuilder.append("\"");
                }
            }
        }
        stringBuilder.append("}");
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        IAttributeAssemble attributeAssemble = new CommonAttributeAssemble();
        attributeAssemble.add(new CommonAttributeDesc("a", 1, false));
        System.out.println(convertToJsonString(attributeAssemble));
        attributeAssemble.add(new CommonAttributeDesc("b", "aa", true));
        attributeAssemble.add(new CommonAttributeDesc("c", new Date(), true));
        attributeAssemble.add(new CommonAttributeDesc("d", true, false));
        System.out.println(convertToJsonString(attributeAssemble));
    }
}
